package com.irssproject.app.service.recommendations.impl;

import com.irssproject.app.dao.recomanacions.iface.RecommendationsDAO;
import com.irssproject.app.entitats.*;
import com.irssproject.app.entitats.recommendations.UserClustersRecommendation;
import com.irssproject.app.service.lectorRSS.iface.SubscriptionsService;
import com.irssproject.app.service.recommendations.iface.RecommendationsService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT)
public class RecommendationsServiceImpl implements RecommendationsService {

    private SubscriptionsService subscripcioService;
    static final Logger logger = Logger.getLogger(RecommendationsServiceImpl.class.getName());
    private RecommendationsDAO recomanacionsDAO;

    public void setRecommendationsDAO(RecommendationsDAO recomanacionsDAO) {
        this.recomanacionsDAO = recomanacionsDAO;
    }

    public void setSubscriptionsService(SubscriptionsService subscripcioService) {
        this.subscripcioService = subscripcioService;
    }

    @Override
    public void refreshSubscripcio(Subscription subscripcio) {
        subscripcioService.refreshSubscription(subscripcio.getId());
    }

    @Override
    public List<User> getNeighbors(User usuari) throws Exception {
        return recomanacionsDAO.getNeighbors(usuari);
    }

    @Override
    public List<UserClustersRecommendation> getTopClusters(User user) {
        return recomanacionsDAO.getTopClusters(user);
    }

    @Override
    public List<Item> getTopItemsByTopic(User user, Long id) {
        return recomanacionsDAO.getTopItemsByCluster(user, id);
    }

    @Override
    public List<Item> getTopItemsByTopic(User user, String topic) {

        List<Topic> lstTopic = recomanacionsDAO.getTopicLstLikeName(topic);
        List<Item> lstItem = new ArrayList<Item>();
        for (Topic t : lstTopic) {
            lstItem.addAll(recomanacionsDAO.getTopItemsByCluster(user, t.getId()));
        }
        Collections.sort(lstItem);
        return lstItem;
    }

    @Override
    public List<Feed> getTopFeeds(User user) {
        return recomanacionsDAO.getTopFeeds(user);
    }

    @Override
    public List<Item> getTopItemsByFeed(User user, Long idFeed) {
        return recomanacionsDAO.getTopItemsByFeed(user, idFeed);
    }

    @Override
    public List<Item> getTopItems(User user) {
        return recomanacionsDAO.getTopItems(user);
    }

    @Override
    public List<Item> getPersItemsByCluster(Long conceptId, Long userId) {
        return recomanacionsDAO.getPersItemsByCluster(conceptId, userId);
    }

    @Override
    public List<UserClustersRecommendation> getMatchingClusters(User connectedUser, User recommendedUser) {
        return recomanacionsDAO.getMatchingClusters(connectedUser, recommendedUser);
    }

    @Override
    public List<Item> fillPersItemsByCluster(Long conceptId, Long userId, String[] items) {
        return recomanacionsDAO.fillPersItemsByCluster(conceptId, userId, items);
    }

    @Override
    public List<Item> fillTopItemsByCluster(User user, Long conceptId, String[] items) {
        return recomanacionsDAO.fillTopItemsByCluster(user, conceptId, items);
    }

    @Override
    public void createUserSubscription(User user, User userToSubs) throws Exception {
        recomanacionsDAO.createUserSubsription(user, userToSubs);
    }

    @Override
    public void createTopicSubscription(User user, Long topicIdToSubs) throws Exception {
        recomanacionsDAO.createTopicSubsription(user, topicIdToSubs);
    }

    @Override
    public void createTopicSubscription(User user, String topic) throws Exception {
        recomanacionsDAO.createTopicSubsription(user, recomanacionsDAO.getTopicByName(topic));
    }

    @Override
    public List<Item> fillTopItems(User user, String[] items) {
        return recomanacionsDAO.fillTopItems(user, items);
    }

    @Override
    public List<Item> fillTopItemsByFeed(User user, Long feedId, String[] items) {
        return recomanacionsDAO.fillTopItemsByFeed(user, feedId, items);
    }

    @Override
    public List<User> fillNeighbors(User connectedUser, String[] usrs) throws Exception {
        return recomanacionsDAO.fillNeighbors(connectedUser, usrs);
    }

    @Override
    public List<UserClustersRecommendation> fillMatchingClusters(User connectedUser, User recommendedUser, String[] recommendedTopics) {
        return recomanacionsDAO.fillMatchingClusters(connectedUser, recommendedUser, recommendedTopics);
    }

    @Override
    public List<UserClustersRecommendation> getClusters(User connectedUser) throws Exception {
        List<User> ngbsLst = recomanacionsDAO.getNeighbors(connectedUser);
        return recomanacionsDAO.getClusters(connectedUser, ngbsLst);
    }

    @Override
    public List<UserClustersRecommendation> fillClusters(User connectedUser, String[] items) throws Exception {
        List<User> ngbsLst = recomanacionsDAO.getNeighbors(connectedUser);
        return recomanacionsDAO.fillClusters(connectedUser, ngbsLst, items);
    }

    @Override
    public void insertPersonalizedLabel(Subscription subscripcio, String etiquetaPersonalitzada, String concepte) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
